<?php

class McCube_Html_Form_Element_Select extends McCube_Html_Form_Element{

    protected $_valueKey   = '';
    protected $_contentKey = '';
    protected $_options    = array();

    public function setOptions($options=array()){
        $this->_options = $options ? $options : $this->getDatasOnDb();
        return $this;
    }

    public function voidOption($void=true){
        $this->_config['voidOption'] = $void;
        return $this;
    }

    public function onChange($onChange){
        $this->setAttr('onchange', $onChange);
        return $this;
    }

    /** */
    protected function _build(){
        if(!$this->_options)
            $this->setOptions();

        $_options = '';
        if(isset($this->_config['voidOption']))
             $_options .= '<option value="">-</option>';

        $this->_value = (array)$this->_value;

        foreach($this->_options as $option => $value)
            $_options .=
            '<option value="'.$option.'"'
                .(in_array($option, $this->_value) ? ' selected' : '').'>'
                    .$value
            .'</option>'
            ;

        return '<select'.parent::buildAttrs().'>'
            .$_options
        .'</select>';
    }
}